package com.yyt.libsrc.net;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.yyt.libsrc.helper.LocalHelper;
import com.yyt.libsrc.helper.MsgHelper;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;

import top.lixb.libcommon.base.BaseApplication;
import top.lixb.libcommon.dialog.IOSAlertDialog;
import top.lixb.libcommon.log.L;
import top.lixb.libcommon.net.CommonCallback;

public abstract class SuccessCallback<T> extends CommonCallback {

    private IOSAlertDialog mBuilder;

    @Override
    public void onRecv(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getBoolean("success")) { //返回成功信息
                T t = new Gson().fromJson(response, getTClass());
                onSuccess(t);
            } else {
                String errMsg = jsonObject.getString("errMsg");
                String errCode = jsonObject.getString("errCode");
                if ("多账号登录".equals(errMsg)||"多账号登陆".equals(errMsg)||errMsg.contains("多账户")||errMsg.contains("多账号")|| TextUtils.equals("USER.0001",errCode)) {
                    Activity curActivity = BaseApplication.getApp().getCurActivity();
                    if (null!=curActivity && !curActivity.isFinishing() ) {
                        mBuilder = new IOSAlertDialog(curActivity).builder();
                        mBuilder.setMsg("当前账号在多台终端登录,\n如密码泄露,请修改密码").setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO: 2018/8/10 这里会导致WindowLeaked
                                if (null != mBuilder) {
                                    mBuilder.cancel();
                                }
                                LocalHelper.saveTicket("");
                                RouterHelper.getInstance().startAct(RouteTable.main_login, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            }
                        }).setCancelable(false).show();
                    }
                }else {
                    MsgHelper.showErrorMsg(errMsg);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            L.e("解析数据错误:"+e.getMessage());
        }
    }

    protected Class<T> getTClass() {
        Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return tClass;
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        MsgHelper.showErrorMsg("网络错误,请检查您的网络连接");
    }

    public abstract void onSuccess(T t);
}
